window.UEDITOR_HOME_URL = ROOTSources + "/dist/neditor";

document.querySelector("#cancel").onclick = function () {
    window.history.go(-1);
};
let timer = null;
let submitBtn = document.querySelector("#submit");
submitBtn.onclick = function () {
    let content = UE.getEditor('editor').getContent();
    let title = document.querySelector("#dynamicTitle").value;
    submitBtn.disabled = true;
    if (timer === null) {
        submitBtn.innerText = "保存中";
        // submitBtn.style.cursor = "not-allowed";
        // pointer-events: none;
        let count = 0;
        btnTimer = setInterval(function () {
            if (count < 3) {
                submitBtn.innerText += ".";
                count++;
            } else {
                count = 0;
                submitBtn.innerText = "保存中";
            }
        }, 500);
    }
    // ajax请求
    let sendContent = {
        "dynamicTitle": title,
        "dynamicContent": content
    };
    let url = ROOTSources + "/dynamic/add";
    let ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
    ajax.open("POST", url, true);
    ajax.setRequestHeader("Content-Type", "application/json");
    ajax.send(JSON.stringify(sendContent));
    ajax.onreadystatechange = function () {
        if (ajax.readyState === 4) {
            if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                let result = JSON.parse(ajax.responseText);
                if (result.status === 1) {
                    window.location.href = ROOTSources + "/dynamic";
                } else {
                    alert("发表动态失败，请重试");
                }
                clearInterval(timer);
                submitBtn.removeAttribute("disabled");
            }
        }
    }
};

//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
var ue = UE.getEditor('editor', {
    // UEDITOR_HOME_URL: ,
    //浮动时工具栏距离浏览器顶部的高度，用于某些具有固定头部的页面
    topOffset: 0,
    //编辑器底部距离工具栏高度(如果参数大于等于编辑器高度，则设置无效)
    toolbarTopOffset: 400,
    initialFrameWidth: "100%",  //初始化编辑器宽度,默认1000
    initialFrameHeight: 500,  //初始化编辑器高度,默认320
    elementPathEnabled: false, // 是否启用元素路径，默认是显示
    wordCount: false, // 是否开启字数统计
    autoHeightEnabled: true,
    autoFloatEnabled: false, // 是否自动浮动
    toolbars: [
        [
            "fullscreen",
            "source",
            "|",
            "undo",
            "redo",
            "|",
            "bold",
            "italic",
            "underline",
            "fontborder",
            "strikethrough",
            "superscript",
            "subscript",
            "removeformat",
            "formatmatch",
            "blockquote",
            "pasteplain",
            "|",
            "forecolor",
            "backcolor",
            "insertorderedlist",
            "insertunorderedlist",
            "|",
            "rowspacingtop",
            "rowspacingbottom",
            "lineheight",
            "|",
            "insertcode",
            "customstyle",
            "paragraph",
            "fontfamily",
            "fontsize",
            "|",
            "directionalityltr",
            "directionalityrtl",
            "indent",
            "|",
            "justifyleft",
            "justifycenter",
            "justifyright",
            "justifyjustify",
            "|",
            // 图片
            // "simpleupload",
            // "imagenone",
            // "imageleft",
            // "imageright",
            // "imagecenter",
            "|",
            "horizontal",
            "inserttable",
            "deletetable",
            "mergecells",
            "splittocells",
        ]
    ]
});
// 初始化提示框文字
UE.getEditor('editor').setPlaceholder('说点儿什么吧');
// 初始化媒体上传路径========================================================== 未完成 =====
UE.Editor.prototype.getActionUrl = function (action) {
    console.log("asd");
    /* 按config中的xxxActionName返回对应的接口地址 */
    if (action === 'uploadimage' || action === 'uploadscrawl') {
        return ROOTSources + '/dynamic/addPic';
    } else if (action === 'uploadvideo') {
        return ROOTSources + '/dynamic/addVideo';
    } else {
        return this._bkGetActionUrl.call(this, action);
    }
};